package com.heykui.restfulcrud.Exception;

import javax.servlet.http.HttpServletRequest;

import com.heykui.restfulcrud.result.Result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class Handler {
    private static final Logger logger = LoggerFactory.getLogger(Handler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(HttpServletRequest req, Exception e){
        StringBuffer logStr = new StringBuffer("服务异常：[");
        logStr.append(req.getRequestURI() + "]\n");
        logStr.append(e.toString());
        logger.error(logStr.toString());
        return Result.error();
    }

    @ExceptionHandler(value = Base.class)
    @ResponseBody
    public Result DiyExceptionHandler(HttpServletRequest req, Base e){
        return new Result(e.errcode, e.msg);
    }

    @ExceptionHandler(value = Err.class)
    @ResponseBody
    public Result ErrHandler(HttpServletRequest req, Err e){
        logger.info(e.msg);
        return new Result(e.errcode, e.msg);
    }

    @ExceptionHandler(value = Suss.class)
    @ResponseBody
    public Result SussHandler(HttpServletRequest req, Suss e){
        return new Result(e.errcode, e.msg, e.data);
    }



}